import router from '@/router';
import { userStore } from '@/store';
import { storeToRefs } from 'pinia';
import io from 'socket.io-client';
export default {
    //@ts-ignore
    install: (app) => {
        const socket = io('http://localhost:3000', { autoConnect: true });
        const user=userStore()
        socket.on('connect', () => {
            socket.emit('login', { id: userStore().userInfo.id })
        })
        socket.on('message', (data: any) => {
            if (!user.chaterInfoList.some(item=>item.id==data.senderId)) {  
                user.getChaterInfo(data.senderId)
                user.addMessageInfo(data.senderId)
            }
            console.log(user.messageList);
            const path=router.currentRoute.value.path;
            if (path!=='/ChatInterface') {
                user.addUnReadNum(data.senderId)
                console.log(storeToRefs(user).chaterInfoList.value);
            }
            if (data.senderId !== user.userInfo.id) {
                user.addMessage(data,data.senderId)
            }
        })
        socket.on('test',(data)=>{
            console.log(data);
            
        })
        app.config.globalProperties.$socket = socket
        app.provide('socket', socket)
    
    }
}
